﻿namespace OnnxOCRApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var bathPath = Path.Combine(AppContext.BaseDirectory, "ONNXModel");
            // 模型和字典文件路径
            string detModelPath = Path.Combine(bathPath, "PP-OCRv4_det.onnx");
            string recModelPath = Path.Combine(bathPath, "PP-OCRv4_rec.onnx"); 
            string dictPath = Path.Combine(bathPath, "ppocrv4_doc_dict.txt");

            // 初始化OCR
            var ocr = new PaddleOCR(detModelPath, recModelPath, dictPath);

            // 处理图像
            string imagePath = Path.Combine(bathPath, "pdfToImg_0.png");
            var results = ocr.Process(imagePath);

            // 过滤结果 - 移除过短或可能是噪声的文本
            var filteredResults = results
                .Where(r => !string.IsNullOrWhiteSpace(r.Text) && r.Text.Length > 1)
                .OrderBy(r => r.Box[1]) // 按照y坐标排序，从上到下
                .ToList();

            // 输出结果
            Console.WriteLine($"识别到 {results.Count} 个文本区域，过滤后 {filteredResults.Count} 个:");
            foreach (var result in filteredResults)
            {
                Console.WriteLine($"文本: {result.Text}");
                Console.WriteLine($"位置: [{string.Join(", ", result.Box)}]");
                Console.WriteLine();
            }

            // 保存处理后的图像以便调试
            ocr.SaveDebugImage(imagePath, Path.Combine(bathPath, "debug_output.png"), filteredResults);
        }
    }
}
